package cn.wen.javax.servlet.jsp.tagext.builder.form;

import java.util.Map;

import cn.wen.javax.servlet.jsp.tagext.builder.base.AbstractBuilder;
import cn.wen.javax.servlet.jsp.tagext.form.SelectTag;

public class SelectBuilder extends AbstractBuilder {

    public SelectBuilder(SelectTag tag) {
	super(tag);
	this.tag = tag;
    }

    private SelectTag tag;

    @Override
    public void process() {
	context.append("<select ");
	Map<String, String> attrs = tag.getAttrs();

	for (String attr : attrs.keySet()) {
	    context.append(attr).append("=\"").append(attrs.get(attr)).append("\" ");
	}
	context.append("> ");

	if (tag.isShowAll()) {
	    context.append("<option value=\"-1\" >全部</option> ");
	}
	for (Map<String, ?> option : tag.getOptionList()) {
	    context.append("<option value=\"").append(option.get("value")).append("\" ");
	    if (option.get("value").equals(tag.getValue())) {
		context.append("selected=\"selected\" ");
	    }
	    context.append(">").append(option.get("desc")).append("</option> ");
	}
	context.append("</select> ");
    }

}
